package org.example.game.history;

import java.util.ArrayDeque;
import java.util.Deque;

/**
 * @Description: TODO
 * @Author wyatt
 * @Data 2024/06/08 22:34
 */
public class Solution100297 {

    public static void main(String[] args) {
        Solution100297 solution = new Solution100297();
        System.out.println(solution.findWinningPlayer(new int[]{4,2,6,3,9}, 2));
    }

    public int findWinningPlayer(int[] skills, int k) {

        if(k >= skills.length){
            int max = 0;
            int maxIndex = 0;
            for (int i = 0; i < skills.length; i++) {
                if(skills[i] > max){
                    max = skills[i];
                    maxIndex = i;
                }
            }

            return maxIndex;
        }


        //模拟

        // 使用ArrayDeque创建队列
        Deque<Integer> queue = new ArrayDeque<>();
        int countK = 0;
        queue.add(0);
        for (int i = 1; i < skills.length; i++) {
            if(skills[i] > skills[queue.peek()]){
                int lastFirst = queue.peek();
                queue.removeFirst();
                queue.addFirst(i);
                queue.addLast(lastFirst);
                countK = 1;
            }else {
                queue.addLast(i);
                countK++;
            }

            if(countK == k){
                return queue.peek();
            }
        }
        return queue.peek();

    }

}
